package com.way.common.core.domain;

import com.way.common.core.enums.ResultCode;
import lombok.Data;

/**
 * 返回结果
 *
 * @param <T> 返回结果类型
 * @author lewis
 * @Date 2024-12-07
 */
@Data
public class R<T> {

    private Integer code;
    private String message;
    private T data;

    public static <T> R<T> ok() {
        return ok(null);
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, ResultCode.SUCCESS);
    }

    public static <T> R<T> fail(String message) {
        return restResult(null, ResultCode.ERROR.getCode(), message);
    }

    public static <T> R<T> fail(ResultCode resultCode) {
        return restResult(null, resultCode);
    }

    private static <T> R<T> restResult(T data, ResultCode resultCode) {
        return restResult(data, resultCode.getCode(), resultCode.getMessage());
    }

    private static <T> R<T> restResult(T data, Integer code, String message) {
        R<T> apiResult = new R<T>();
        apiResult.setCode(code);
        apiResult.setMessage(message);
        apiResult.setData(data);
        return apiResult;
    }

}
